<?php

namespace app\admin\controller;

use app\admin\model\Community;
use app\admin\model\Form4Childen;
use app\admin\model\Form7;
use app\admin\model\TemperatureAlarm;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\Exception;

/**
 * @ControllerAnnotation(title="密接人员管理")
 */
class Form4 extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Form4();

        $this->assign('getSexList', $this->model->getSexList());
        
        $this->assign('getLevelList', $this->model->getLevelList());

        $this->assign('getCommunityList', Community::getCommunityList());
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();

            if (!$this->isSuper) {
                $where[] = ['community', '=', $this->user->community_id];
            }

            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'name|姓名'                          =>  'require|chs',
                'sex|性别'                           =>  'require',
                'id_card|身份证号'                   =>  'require|idCard',
                'living|现居住地址'                  =>  'require',
                'domicile|户籍所在地'                =>  'require',
                'employer|工作单位'                  =>  'require',
                'level|密接等级'                     =>  'require',
                'mobile|手机号码'                    =>  'require|mobile',
            ];
            $this->validate($post, $rule);

            if (!$this->isSuper) {
                $post['community'] = $this->user->community_id;
            } else {
                empty($post['community']) && $this->error('居住地所属社区不能为空');
            }

            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $row = $this->model->find($this->request->param($this->model->getPk()));
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'name|姓名'                          =>  'require|chs',
                'sex|性别'                           =>  'require',
                'id_card|身份证号'                   =>  'require|idCard',
                'living|现居住地址'                  =>  'require',
                'domicile|户籍所在地'                =>  'require',
                'employer|工作单位'                  =>  'require',
                'level|密接等级'                     =>  'require',
                'mobile|手机号码'                    =>  'require|mobile',
            ];
            $this->validate($post, $rule);

            if (!$this->isSuper) {
                $post['community'] = $this->user->community_id;
            } else {
                empty($post['community']) && $this->error('居住地所属社区不能为空');
            }

            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除")
     */
    public function delete()
    {
        $ids = $this->request->param($this->model->getPk());
        $row = $this->model->whereIn('id', $ids)->select();
        $row->isEmpty() && $this->error('数据不存在');

        //是否有接触者绑定
        if (Form4Childen::whereIn('form4_id', $ids)->count()) {
            $this->error('删除失败：请先清空名下接触者');
        }

        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

    /**
     * @NodeAnotation(title="接触者")
     */
    public function touch_index()
    {
        return $this->min_menu_index(Form4Childen::class, 'form4_id', ['form4']);
    }

    /**
     * @NodeAnotation(title="新增接触者")
     */
    public function touch_add()
    {
        $rule = [
            'name|姓名'             =>  'require|chs',
            'sex|性别'              =>  'require',
            'id_card|身份证号'      =>  'require|idCard',
            'mobile|手机号'         =>  'require|mobile',
            'living|现居住地址'     =>  'require',
        ];
        return $this->min_menu_add(Form4Childen::class, 'form4_id', $rule);
    }

    /**
     * @NodeAnotation(title="编辑接触者")
     */
    public function touch_edit()
    {
        $rule = [
            'name|姓名'             =>  'require|chs',
            'sex|性别'              =>  'require',
            'id_card|身份证号'      =>  'require|idCard',
            'mobile|手机号'         =>  'require|mobile',
            'living|现居住地址'     =>  'require',
        ];
        return $this->min_menu_edit(Form4Childen::class, $rule);
    }

    /**
     * @NodeAnotation(title="删除接触者")
     */
    public function touch_delete()
    {
        return $this->min_menu_delete(Form4Childen::class);
    }

    /**
     * @NodeAnotation(title="体温记录")
     */
    public function temperature_index()
    {
        return $this->temperature_index_common('form4');
    }

    /**
     * @NodeAnotation(title="添加体温记录")
     */
    public function temperature_add()
    {
        return $this->temperature_add_common();
    }
    
}